#individual level d-i-d

css2<- read.csv("css05_cv.csv")
css2$part<- css2$cash + css2$vouchers
css2$part[css2$part==2]<-1
css2$part<- css2$part*100

css2$voucher_yr<-0
css2$voucher_yr[css2$elec_year>2016]<-1
css2$republican<- 0
css2$republican[css2$party=="Democratic"]<- 0
css2$republican[css2$party=="Republican"]<- 1
css2$republican[css2$party=="Non-Partisan"]<- 0
css2$race<- factor(css2$race, levels = c("European","East and South Asian", "Hispanic and Portuguese", "Likely African-American","Other"))
css2$iswhiteperson<- 0
css2$iswhiteperson[css2$race=="European"]<- 1
css2$iswhiteperson[is.na(css2$race)]<- NA
css2$income<- as.numeric(paste0(substr(css2$income,2,nchar(css2$income))))
css2$income_perc<- ceiling(ecdf(css2$income)(css2$income)*10)
css2$amount<- css2$cash_amt + css2$vouchers_amt

css3<-read.csv("css11_cv.csv")
css3$voucher_yr<-0
css3$voucher_yr[css3$elec_year>2015]<-1
css3$part<- css3$cash + css3$vouchers
css3$part[css3$part==2]<-1
css3$part<- css3$part*100
css3$age_dec<- ceiling(ecdf(css3$age_at_elecyr)(css3$age_at_elecyr)*10)
css3$amount<- css3$cash_amt + css3$vouchers_amt


#t4 = in text table 4
#ta11 = appendix table 11
#ta12 = appendix table 12

qqq<- felm(part~iswhiteperson*voucher_yr + income_perc*voucher_yr+sums*voucher_yr+republican*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2)
qqq1<- felm(part~iswhiteperson*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2)
qqq2<- felm(part~  income_perc*voucher_yr+as.factor(elec_year)|as.factor(id)|0|0 , data=css2)
qqq3<- felm(part~sums*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2)
qqq4<- felm(part~republican*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2)
#the location of as.factor(elec_year) relative to the pipe isn't mathematically important,
#it just depends on if you want R to output the year FE (qqq2's year FE are mentioned in the main text)

summary(qqq, robust=T) #t4
summary(qqq1, robust=T)#t4
summary(qqq2, robust=T)#t4
summary(qqq3, robust=T)#t4
summary(qqq4, robust=T)#t4


#under 70
qqq5_a<- felm(part~age_at_elecyr*voucher_yr|as.factor(elec_year)+as.factor(id)|0|0 , data=css3[css3$age_at_elecyr<70,])
summary(qqq5_a, robust=T)#t4
#over 70
qqq5_b<- felm(part~age_at_elecyr*voucher_yr|as.factor(elec_year)+as.factor(id)|0|0 , data=css3[css3$age_at_elecyr>=70,])
summary(qqq5_b, robust=T)#t4

#as factor (#ta11)
qqq2f<- felm(part~  as.factor(income_perc)*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2[complete.cases(css2$income_perc),])
qqq3f<- felm(part~as.factor(sums)*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2[complete.cases(css2$sums),])
summary(qqq2f, robust=T)#ta11
summary(qqq3f, robust=T)#ta11

#table 4 but using dollar amounts (ta12)
qqqd<- felm(amount~iswhiteperson*voucher_yr + income_perc*voucher_yr+sums*voucher_yr+republican*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2)
qqq1d<- felm(amount~iswhiteperson*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2)
qqq2d<- felm(amount~  income_perc*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2)
qqq3d<- felm(amount~sums*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2)
qqq7d<- felm(amount~republican*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2)
qqq5d<- felm(amount~age_at_elecyr*voucher_yr|as.factor(elec_year)|0|0 , data=css3[css3$age_at_elecyr<70,])
qqq4d<- felm(amount~age_at_elecyr*voucher_yr|as.factor(elec_year)|0|0 , data=css3[css3$age_at_elecyr>=70,])

summary(qqqd, robust=T)#ta12
summary(qqq1d, robust=T)#ta12
summary(qqq2d, robust=T)#ta12
summary(qqq3d, robust=T)#ta12
summary(qqq7d, robust=T)#ta12
summary(qqq5d, robust=T)#ta12
summary(qqq4d, robust=T)#ta12


#placebo test (table a27)
css2_plac<- subset(css2, css2$elec_year<2016)
css2_plac$voucher_yr[css2_plac$elec_year>2010]<-1 
qqq_pl<- felm(part~iswhiteperson*voucher_yr + income_perc*voucher_yr+sums*voucher_yr+republican*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2_plac)
qqq1_pl<- felm(part~iswhiteperson*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2_plac)
qqq2_pl<- felm(part~  income_perc*voucher_yr+as.factor(elec_year)|as.factor(id)|0|0 , data=css2_plac)
qqq3_pl<- felm(part~sums*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2_plac)
qqq4_pl<- felm(part~republican*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2_plac)
summary(qqq_pl, robust=T) 
summary(qqq1_pl, robust=T)
summary(qqq2_pl, robust=T)
summary(qqq3_pl, robust=T)
summary(qqq4_pl, robust=T)

